﻿using AntdUI;
using MyChat.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;

namespace MyChat
{
    public partial class ModbusTcpView : AntdUI.Window
    {
        private ModbusViewModel _viewModel;
        /// <summary>
        /// 监听状态
        /// </summary>
        private bool IsEnable = false;
        public ModbusTcpView()
        {
            InitializeComponent();

            _viewModel = new ModbusViewModel();
            this.DataBindings.Add("Text", _viewModel, "StatusMessage");

            

            // 绑定 ReadData 到 TextBox 控件
            //txtReadData.DataBindings.Add("Text", _viewModel, "ReadData");
            //viewModel = new Form2();

            //AntdUI.Button button = new AntdUI.Button 
            //{
            //    Text="IP",
            //    Width = 200,
            //    Location = new System.Drawing.Point(100, 100),  // 控件位置
            //};
            //this.Controls.Add(button);
        }



        private void pageHeader_Modbus_Click(object sender, EventArgs e)
        {

        }

        private void pageHeader_Modbus_BackClick(object sender, EventArgs e)
        {
            this.Hide();  // 隐藏 Form2
            Main main = new Main();
            main.Show();  // 显示 Form1

        }

        private void button_open_Click(object sender, EventArgs e)
        {
            if (IsEnable)
            {
                //MessageBox.Show("请勿重复建立连接!");
                AntdUI.Notification.warn(this, "警告", "请勿重复建立连接", AntdUI.TAlignFrom.Top, autoClose: 3);
                return;
            }
            string ip = input_ip.Text.Trim();
            
            if (ip == null)
            {
                //MessageBox.Show("ip不能为空!");
                AntdUI.Notification.warn(this, "警告", "ip不能为空", AntdUI.TAlignFrom.Top, autoClose: 3);
                return;
            }
            if (string.IsNullOrEmpty(input_port.Text))
            {
                AntdUI.Notification.warn(this, "警告", "端口不能为空", AntdUI.TAlignFrom.Top, autoClose: 3);
                return;
            }
            int port = Convert.ToInt32(input_port.Text.Trim());
            _viewModel.IpAddress = ip;
            _viewModel.Port = port;
            _viewModel.Connect();
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            _viewModel.Disconnect();
        }
    }
}
